export default {
  // 为当前模块开启命名空间
  namespaced: true,

  // 模块的 state 数据
  state: () => ({
    trendList: [],
  }),

  // 模块的 mutations 方法
  mutations: {
    updateTrendList(state, trendList) {
      state.trendList = trendList
      this.commit('m_trend/saveTrendListToStorage')
    },
    // updateTrendList(state, trend) {
    //   state.trendList.unshift(trend); // 将 trend 对象添加到数组前面
    //   this.commit('m_trend/saveTrendListToStorage')
    // },
    addTrendListLikeNum(state, trendId) {
      const targetIndex = state.trendList.findIndex(trend => trend.id === trendId);
      if (targetIndex !== -1) {
        state.trendList[targetIndex].likeNum++; // 找到目标对象后，将其 likeNum 值加 1
      }
      // console.log(state.trendList[targetIndex])
      this.commit('m_trend/saveTrendListToStorage')
    },
    deleteTrendListLikeNum(state, trendId) {
      const targetIndex = state.trendList.findIndex(trend => trend.id === trendId);
      if (targetIndex !== -1) {
        state.trendList[targetIndex].likeNum--; // 找到目标对象后，将其 likeNum 值加 1
      }
      // console.log(state.trendList[targetIndex])
      this.commit('m_trend/saveTrendListToStorage')
    },
    // deleteTrendListItem(state, index) {
    //   state.trendList.splice(index, 1);
    //   this.commit('m_trend/saveTrendListToStorage')
    // },
    // 定义将 trendList数组 持久化存储到本地 mutations 方法
    saveTrendListToStorage(state) {
      uni.setStorageSync('trendList', JSON.stringify(state.trendList))
    },
  },

  // 模块的 getters 属性
  getters: {

  },
}